import java.util.ArrayList;
import java.util.List;

public class num77 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        int[] nums = new int[n];
        for (int i = 1; i <= n; i++) {
            nums[i - 1] = i;
        }
        boolean[] b = new boolean[n];
        dfs(nums,k,0,path,res,b);
        return res;
    }

    private void dfs(int[] nums, int k, int depth, List<Integer> path, List<List<Integer>> res,boolean[] b) {
        if(depth == k) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(!b[i]) {
                if(path.isEmpty()) {
                    path.add(nums[i]);
                    b[i] = true;
                    dfs(nums,k,depth + 1,path,res,b);
                    b[i] = false;
                    path.remove(path.size() - 1);
                } else if(nums[i] > path.get(path.size() - 1)) {
                    path.add(nums[i]);
                    b[i] = true;
                    dfs(nums,k,depth + 1,path,res,b);
                    b[i] = false;
                    path.remove(path.size() - 1);
                }
            }
        }
    }
}
